/**
*      author: Y_Yao
*      created: 2024-12-05 23:01:26
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 7;
int a[N];
string s;
int dp[N];

void solve()
{
    cin >> s;
    vector<int> v;
    int n = s.size();
    s = " " + s;
    int sum = 0;
    int c1 = 0;
    int c2 = 0;
    for(int i = 1;i <= n;i++)
    {
        sum += s[i] - '0';
        if(s[i] == '2') c1++;
        if(s[i] == '3') c2++;
    }

    for(int i = 0;i <= min(c1,9LL);i++)
    {
        for(int j = 0;j <= min(c2,9LL);j++)
        {
            if((sum + i * 2 + j * 6) % 9 == 0)
            {
                cout << "YES" << endl;
                return;
            }
        }
    }
    cout << "NO" << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}